package com.baselibrary.libs.Utils.UtilsTimer;

import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;

/**
 * Created by Administrator 于萌萌
 * 创建日期: 16:12 . 2017年02月23日
 * 描述:
 * <p>
 * <p>
 * 备注:
 */

public abstract class sssss {
    /**结束时间
     * Millis since epoch when alarm should stop.
     */
    private final long mMillisInFuture;

    /**
     * 间隔
     * The interval in millis that the user receives callbacks
     */
    private final long mCountdownInterval;
    //相对结束时间
    private long mStopTimeInFuture;

    /**取消标记
     * boolean representing if the timer was cancelled
     */
    private boolean mCancelled = false;

    /**
     *
     * @param millisInFuture 结束时间
     * @param countDownInterval 间隔
     */
    public sssss(long millisInFuture, long countDownInterval) {
        mMillisInFuture = millisInFuture;
        mCountdownInterval = countDownInterval;
    }

    /**取消倒计时的方法
     * Cancel the countdown.
     */
    public synchronized final void cancel() {
        //取消标记 true
        mCancelled = true;
        mHandler.removeMessages(MSG);
    }

    /**开始倒计时的方法
     * Start the countdown.
     */
    public synchronized final sssss start() {
        //取消标记 设置为false
        mCancelled = false;
        //结束时间 如果小于 等于 0 我就  调用 抽象的结束方法 子类 处理 逻辑
        if (mMillisInFuture <= 0) {
            onFinish();
            return this;
        }
        // 当前时间 + 结束时间 = 相对结束时间
        mStopTimeInFuture = SystemClock.elapsedRealtime() + mMillisInFuture;
        //发送开始 信号
        mHandler.sendMessage(mHandler.obtainMessage(MSG));
        return this;
    }


    /**
     * Callback fired on regular interval.
     * @param millisUntilFinished The amount of time until finished.
     */
    public abstract void onTick(long millisUntilFinished);

    /**
     * Callback fired when the time is up.
     */
    public abstract void onFinish();


    private static final int MSG = 1;


    // handles counting down
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            //同步代码块
            synchronized (sssss.this) {
                //如果是 已经 取消的 就返回
                if (mCancelled) {
                    return;
                }
                //相对结束时间  - 当前时间  =  相对间隔时间
                final long millisLeft = mStopTimeInFuture - SystemClock.elapsedRealtime();
                //如果相对间隔时间小等于 0  就是 完成了
                if (millisLeft <= 0) {
                    onFinish();
                    //如果 相对间隔时间 小于 间隔
                } else if (millisLeft < mCountdownInterval) {
                    //延时发送 倒计时 消息 延时的秒 是 相对间隔时间
                    // no tick, just delay until done
                    sendMessageDelayed(obtainMessage(MSG), millisLeft);
                } else {
                    //如果 相对间隔时间 大于等于 间隔

                    //当前时间
                    long lastTickStart = SystemClock.elapsedRealtime();
                    //回调 相对结束时间
                    onTick(millisLeft);

                    // take into account user's onTick taking time to execute
                    //间隔时间 + 间隔 - 当前时间 = 相对间隔时间
                    long delay = lastTickStart + mCountdownInterval - SystemClock.elapsedRealtime();

                    // special case: user's onTick took more than interval to
                    // complete, skip to next interval
                    //如果 相对间隔时间  小于0 就 += 间隔
                     while (delay < 0) delay += mCountdownInterval;
                    //在发一个 相对间隔时间
                    sendMessageDelayed(obtainMessage(MSG), delay);
                }
            }
        }
    };
}
